package com.flowable.listener;

import com.flowable.mapper.LeaveMapper;
import com.flowable.pojo.Leave;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;

/**
 * 长沙先飞信息科技有限公司<br>
 * Created with IntelliJ IDEA.<br>
 * Date:2024/7/8<br>
 * Time:12:49<br>
 * 功能描述：<br>
 *
 * @author huang hou chun
 * @since JDK 1.8 <br>
 * Copyright (c) 2023 www.csxfrj.com System Incorporated All Rights Reserved.
 */
@Component
public class LeaveStartListener implements ExecutionListener {

    @Resource
    private LeaveMapper leaveMapper;

    @Override
    public void notify(DelegateExecution execution) {
        execution.setVariable("leave", createLeave(execution));
    }
    
    private Leave createLeave(DelegateExecution execution) {
        Leave leave = new Leave();
        leave.setProcessInstanceId(execution.getProcessInstanceId());
        leave.setStartTime(execution.getVariable("startTime", LocalDate.class));
        leave.setEndTime(execution.getVariable("endTime", LocalDate.class));
        leave.setReason(execution.getVariable("reason", String.class));
        leaveMapper.insert(leave);
        return leave;
    }
}
